//
//  HomeTopView.swift
//  weiboLQ
//
//  Created by 半缘君 on 15/12/11.
//  Copyright © 2015年 banyuanjun.cn. All rights reserved.
//

import UIKit

class HomeTopView: UIView {
    
    var viewModel:StatusViewModel?{
        didSet {
//            let SViewModel = StatusViewModel(status: status!)
//            let urlString = status?.user?.profile_image_url ?? ""
            iconImage.sd_setImageWithURL(viewModel!.URLForIconImge, placeholderImage:viewModel!.defaultIconImage)
            nameLabel.text = viewModel!.textForNameLable
            
            levelIcon.image = viewModel!.imageForLevel

            vipIcon.image = viewModel!.imageForVip
            
//            timeLabel.text = SViewModel.textForTimeLabel
//            sourceLabel.text = SViewModel.textForSourceLabel
        }
    }
    
    //MARK: - 懒加载控件
    /// 头像
    lazy var iconImage = UIImageView(imageName: "common_icon_membership_level2")
    /// 昵称
    lazy var nameLabel = UILabel(txt: "张三", txtFont: 12)
    /// 等级
    lazy var levelIcon = UIImageView(imageName: "common_icon_membership_level2")
    /// vip标志
    lazy var vipIcon = UIImageView(imageName: "common_icon_membership_level2")
    /// 时间
    lazy var timeLabel = UILabel(txt: "刚刚", txtFont: 12)
    /// 来源
    lazy var sourceLabel = UILabel(txt: "来自 黑马", txtFont: 12)
    /// 构造方法
    override init(frame: CGRect) {
      super.init(frame: frame)
//        backgroundColor = UIColor(white: 0.95, alpha: 1)
        setupUI()
  
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

//添加控件并设置约束
extension HomeTopView {
    
    func setupUI () {
        addIconImage()
        addNameLabel()
        addLevelIcon()
        addTimeLabel()
        addSourceLabel()
        addVipImage()
    }

/// 添加头像
    private func addIconImage (){
        addSubview(iconImage)
        iconImage.snp_makeConstraints{ (make) -> Void in
            make.top.equalTo(self).offset(LQHomeCellMargin)
            make.left.equalTo(self).offset(LQHomeCellMargin)
            make.width.equalTo(45)
            make.height.equalTo(45)
            }

    
    }
/// 添加nameLabel
    private func addNameLabel () {
        addSubview(nameLabel)
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage)
            make.left.equalTo(iconImage.snp_right).offset(LQHomeCellMargin)
        }
    }
/// 等级label
    private func addLevelIcon () {
        addSubview(levelIcon)
        levelIcon.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(LQHomeCellMargin)
            make.top.equalTo(nameLabel)
        }
    }
/// 时间label
    private func addTimeLabel () {
    addSubview(timeLabel)
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(iconImage)
        }
    
    }
/// 来源label
    private func addSourceLabel () {
        addSubview(sourceLabel)
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp_right).offset(LQHomeCellMargin)
        }
    }
/// vip图标
    private func addVipImage () {
        addSubview(vipIcon)
        
        vipIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_right)
            make.centerY.equalTo(iconImage.snp_bottom)
        }
    }
    
}